package com.baturu.vin.gm.script;

import com.baturu.vin.gm.script.DAO.PartsDAO;
import com.baturu.vin.gm.script.DTO.PartsDTO;
import com.baturu.vin.test.AbstractBaseCase;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @Author: chenjiahao
 * @Time: 2019/3/22
 */
public class Sort extends AbstractBaseCase {

    @Autowired
    private PartsDAO partsDAO;

    @Test
    public void sortBySeries() throws Exception {
        Integer start = 0;
        Integer maxId = partsDAO.queryMaxId();
        for (Integer i = 1; i < maxId; i++) {
            if (i % 10000 == 0) {
                List<PartsDTO> partsDTOs = partsDAO.queryParts(start, i);
                if (CollectionUtils.isNotEmpty(partsDTOs)) {
                    List<PartsDTO> l = Lists.newArrayList();
                    partsDTOs.forEach(d -> {
                        List<String> series = Splitter.on(",").splitToList(d.getOrderKey());
                        series.forEach(q -> {
                            PartsDTO build = PartsDTO.builder().brand(d.getBrand()).imageCode(d.getImageCode()).orderKey(q).partsCode(d.getPartsCode()).partsName(d.getPartsName()).positionNum(d.getPositionNum()).build();
                            l.add(build);
                        });
                    });
                    partsDAO.insert(l);
                }
                start = i;
            }
        }
    }

}
